<?php
namespace app\common\model;

use think\Model;
use think\Db;
use app\common\model\UserModel;

class UserGradeModel extends Model
{
    protected $createTime = 'create_time';

   
    protected $name = 'user_grade';
    protected $autoWriteTimestamp = true;
    public function list()
    {   
        
        return Db::name('user')
            ->alias('u')
            ->rightJoin('p_user_grade ug', 'ug.uid = u.uid')
            ->field('ug.*, u.uid,u.phone,u.username')
            ->order('ug.id asc');
    }

    // 插入数据
    public function insertGrade($data)
    {
        return $this->saveAll($data);
    }

    // 获取当前牌局战绩
    public function getCurMatch($tid, $match)
    {
        $map = [
            'tid' => $tid,
            'innings' => (int)$match
        ];
        $list = $this->where($map)->all();
        $uModel = new UserModel;
        foreach ($list as $k => &$v) {
            $v['username'] = $uModel->getUserName($v['uid']);
            if ($v['win'] > 0) $v['win'] = '+'.$v['win'];
        }
        return $list;
    }
}
